package com.yang.String_;

public class LeetCode58 {
    public static void main(String[] args) {
        SolutionLeetCode58 solutionLeetCode58 = new SolutionLeetCode58();
        String s = "abcdefg";
        String s1 = solutionLeetCode58.reverseLeftWords(s, 2);
        System.out.println(s1);
    }
}

class SolutionLeetCode58 {
    public String reverseLeftWords(String s, int n) {
        StringBuilder stringBuilder = new StringBuilder(s);
        reverseNWorld(stringBuilder, 0, n-1);
        reverseNWorld(stringBuilder, n,s.length()-1);
        reverseString(stringBuilder);

        return new String(stringBuilder);
    }
    private void reverseString(StringBuilder s){
        int length = s.length();
        reverseNWorld(s, 0, length-1);
    }

    private void reverseNWorld(StringBuilder s, int start, int end){

        while (end > start){
            char c = s.charAt(start);
            s.setCharAt(start, s.charAt(end));
            s.setCharAt(end, c);
            start++;
            end--;
        }
    }
}
